﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.ModelBinding;
using System.Web.UI;
using System.Web.UI.WebControls;
using Box.BL;
using Box.Entities;
using Microsoft.Practices.Unity;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using System.IO;
namespace Box.Cliente.Logistica.Compras
{
    public partial class frmListCompras : System.Web.UI.Page
    {
        [Dependency]
        public ICompraBL compraBl { get; set; }
        [Dependency]
        public IProveedorBL proveedorBl { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            var accion = Request.QueryString["accion"];
            var id = Request.QueryString["id"];
            if (accion == "pdf")
            {
                var identificador = int.Parse(id);
                var compra = compraBl.GetFromComprasById(identificador);
                string num = compra.NumDocumento;
                int idprov = compra.IdProveedor;
                string numDoc = compra.NumDocumento;
                List<DetalleCompra> listaDetalec = compra.DetalleCompras;

                generarPDF(num,idprov,compra.FechaCompra,numDoc,listaDetalec);
            }

        }

        public IEnumerable<Compra> GetCompras([Control("txtCriterio")] string criterio)
        {
            return compraBl.GetFromComprasByCriterio(criterio);
        }

        public void generarPDF(string numDoc, int idProv, DateTime fechaCompra, string numDocs,List<DetalleCompra> listaDetallec)
        {
           

            var proveedor = proveedorBl.GetFromProveedorsById(idProv);
            string nomproveedor = proveedor.NombreEmpresa;


            Document pdfDoc = new Document(PageSize.A4, 10, 10, 10, 10);

            try
            {
                PdfWriter.GetInstance(pdfDoc, System.Web.HttpContext.Current.Response.OutputStream);

                pdfDoc.Open();

                string cadenaFinal = "";
                string encabezado = Server.MapPath("/Imagenes/Logo_pdf.PNG");
                string piepag = Server.MapPath("/Imagenes/Logo_pdf_2.PNG");

                cadenaFinal += "<img src='" + encabezado + "' Height='100' Width='550' /><br/><br/>";
                cadenaFinal += "<TABLE BORDER='1'>" +
                                "<TR>" +
                                    "<TD>SEÑORES: "+nomproveedor+"<br/>"+
                                    "ATENCIÓN: "+proveedor.PropietarioEmpresa+"<br/>"+
                                    "FECHA: " + fechaCompra.ToString()
                                    +"</TD>" +
                                    "<TD>"+
                                    "PROFORMA N°:" + numDocs +
                                    "<br/> <br/> <br/>"
                                    + "</TD>" +
                                "</TR>" +
                                "</TABLE> <br/>";

                cadenaFinal += "<label>De mi especial consideración adjunto proforma de acuerdo a lo solictado.</label> <br/> <br/>";


                cadenaFinal += "<TABLE BORDER='1'>" +
                                "<TR>" +
                                    "<TD>ITEM</TD>" +
                                    "<TD>CANT</TD>" +
                                    "<TD colspan=\"6\">DESCRIPCIÓN</TD>" +
                                    "<TD colspan=\"2\">UND</TD>" +
                                    "<TD colspan=\"2\">PRECIO UNITARIO</TD>" +
                                    "<TD colspan=\"3\">PRECIO TOTAL SOLES</TD>" +
                                "</TR>" +
                                "<TR>" +
                                    "<TD>1</TD>" +
                                    "<TD>10</TD>" +
                                    "<TD colspan=\"6\">guante hycron con lona interior con puño tejido</TD>" +
                                    "<TD colspan=\"2\">DOCENA</TD>" +
                                    "<TD colspan=\"2\">S/. 13.00</TD>" +
                                    "<TD colspan=\"3\">S/. 130.00</TD>"
                                +"</TR>" +
                                "</TABLE> <br/> <br/> <br/> <br/>";

                cadenaFinal += "<br/> <br/> <br/> <br/> <img src='" + piepag + "' Height='100' Width='550' />";

                 
                //Assign Html content in a string to write in PDF 
                string strContent = cadenaFinal;

                //Read string contents using stream reader and convert html to parsed conent 
                var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(strContent), null);

                //Get each array values from parsed elements and add to the PDF document 
                foreach (var htmlElement in parsedHtmlElements)
                    pdfDoc.Add(htmlElement as IElement);

                //Close your PDF 
                pdfDoc.Close();

                Response.ContentType = "application/pdf";

                //Set default file Name as current datetime 
                Response.AddHeader("content-disposition", "attachment; filename=Proforma.pdf");
                System.Web.HttpContext.Current.Response.Write(pdfDoc);

                Response.Flush();
                Response.End();

            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }
    }
}